import datetime

from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

from demo.xueqiuAppium.page.base_page import BasePage
from demo.xueqiuAppium.page.main import Main


class App(BasePage):
	_package = 'com.xueqiuAppium.android'
	_activity = '.view.WelcomeActivityAlias'

	def start(self):
		if self._driver is None:
			caps = {}
			caps["platformName"] = "Android"
			caps["deviceName"] = "hogwards"
			caps["automationName"] = "uiautomator2"
			caps["appPackage"] = self._package
			caps["appActivity"] = self._activity

			# 提速参数
			# caps["noReset"] = True
			# caps["dontStopAppOnReset"] = True
			# caps["skipServerInstallation"] = True

			# 中文
			# caps["unicodeKeyboard"] = True
			# caps["resetKeyboard"] = True

			caps["chromedriverExecutableDir"] = "/usr/local/bin/chromedrivers/all/"
			# caps["chromedriverExecutable"] = "/usr/local/bin/chromedrivers/chromedriver_2.20/chromedriver"
			# caps["chromedriverExecutable"] = "/usr/local/bin/chromedrivers/chromedriver_2.42/chromedriver"
			# caps["avd"] = "6.0"
			self._driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
			self._driver.implicitly_wait(20)
		else:
			print(self.driver)
			self._driver.start_activity(self._package, self._activity)
		return self

	def main(self) -> Main:
		#wait main page
		def wait_load(driver):
			print(datetime.datetime.now())
			source = self._driver.page_source
			if '我的' in source:
				return True
			if '同意' in source:
				return True
			if 'image_cancel' in source:
				return True
			return False

		WebDriverWait(self._driver, 60).until(wait_load)

		return Main(self._driver)
